<?php

/**
 *
 * User: 卖女孩的小火柴
 * Date: 16/11/3
 * Time: 下午2:58
 *
 */
namespace Home\Controller\WorkSummary;

use Home\Controller\BaseController;
use Home\Model\WorkSummary\ClinicModel;
use Home\Model\WorkSummary\DoctorModel;
use Home\Model\WorkSummary\SalemanapplyModel;
use Home\Model\WorkSummary\SalemanModel;
use Home\Model\WorkSummary\SalemansignModel;
use Home\Model\WorkSummary\SalemanorderModel;
use Home\Common\WorksummerHelper;
use Home\Common\SalemanagerHelper;

class SaleSummaryController extends BaseController
{
    private $doctorModel;
    private $clinicModel;
    private $salemanModel;
    private $salemanSignModel;
    private $salemanApplyModel;
    private $sale_id;
    private $role_id;
    private $source;
    public function _initialize()
    {
        parent::_initialize();
        $user = session(C('SESSION_PREFIX').'_salemanager');
        $this->assign('uuid',$user['id']);
        $user['id'] = think_ucenter_encrypt($user['id']);
        $this->assign('myself',$user);
        $user['uid'] = $user['id'];
        $this->doctorModel = new DoctorModel();
        $this->clinicModel = new ClinicModel();
        $this->salemanSignModel = new SalemansignModel();
        $this->salemanApplyModel = new SalemanapplyModel();
        $this->salemanOrderModel = new SalemanorderModel();
        $this->salemanModel = new SalemanModel();
        $this->source = I('get.source');
    }

    /*
     *
     * 业务员工作汇总
     *
     * */
    public function index($sale_id=null,$year=null)
    {
    		if(empty($sale_id) || empty($year)){
    			$sale_id = I('get.saleId');
    			$year = I('get.year');
    		};
    		if(!empty(I('get.saleId'))){
    			$this->sale_id = $sale_id = think_ucenter_decrypt($sale_id);
    		}else{
    			$this->sale_id = $sale_id;
    		}
		$this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
		$sales='';
        $sales = WorksummerHelper::getAllChildrenId($sale_id);
       //加入本人
       if(!empty($sales)){
       	$saleids = $sales.",".$sale_id;
       }else{
       	$saleids =$sale_id;
       }
       
        if( !$year )
        {
            $year = date("Y");
        }
        
        
        //获取该角色所有业务员列表
        if($this->role_id==0||$this->role_id==6){  //如果是总部角色，获取所有的业务员
        		$salemans =$this->salemanModel->where('role_id=5 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();
        }else{
       	 	$salemans =$this->salemanModel->where('id in ('.$saleids.') and role_id=5  and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();	 
        }
        //业务员数量
        if($this->role_id==0||$this->role_id==6){  //如果是总部，获取所有业务员数量
        		$salecount = $this->salemanModel->where(' role_id=5 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        }else{
        	$salecount = $this->salemanModel->where('id in ('.$saleids.') and role_id=5 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        	 
        }
      	//县总数量
      	if($this->role_id==0||$this->role_id==6){
      		$countycount = $this->salemanModel->where(' role_id=4 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
      	}else{
      		$countycount = $this->salemanModel->where('id in ('.$saleids.') and role_id=4 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
      		 
      	}
        
        //地总数量
        if($this->role_id==0||$this->role_id==6){
       	 	$citycount = $this->salemanModel->where(' role_id=3 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        }else{
        		$citycount = $this->salemanModel->where('id in ('.$saleids.') and role_id=3 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count(); 	
        }
        
        //省总数量
        if($this->role_id==0||$this->role_id==6){
        		$provincecount = $this->salemanModel->where(' role_id=2 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        	}else{
        		$provincecount = $this->salemanModel->where('id in ('.$saleids.') and role_id=2 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();		 
        }
        
        //大区经理助理数量
        if($this->role_id==0||$this->role_id==6){
        		$assistantcount = $this->salemanModel->where(' role_id=11 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        }else{
        		if($this->role_id==1){
        			$assistantcount= $this->salemanModel->where('superior_id = ('.$sale_id.') and role_id=11 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
	        	}else{
	        		$assistantcount= $this->salemanModel->where('id in ('.$saleids.') and role_id=11 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
	        	}
        		
        }
        //大区经理数量
        $saleManagercount = $this->salemanModel->where('role_id=1 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->count();
        
        //目标诊所统计
        
        if($this->role_id==0||$this->role_id==6){
       	 	$purposeClinicNum=$this->clinicModel->where(' DATE_FORMAT(add_time,"%Y")='.$year.' and type=1 and is_delete=1')->count();
        }elseif($this->role_id==5||$this->role_id==4||$this->role_id==3){
        		$purposeClinicNum=$this->clinicModel->where('saleman_id ='.$sale_id.' and  DATE_FORMAT(add_time,"%Y")='.$year.' and type=1 and is_delete=1')->count(); 	
        }else{
        		$purposeClinicNum=$this->clinicModel->where('saleman_id in ('.$saleids.') and  DATE_FORMAT(add_time,"%Y")='.$year.' and type=1 and is_delete=1')->count();  	
        }
        //诊所统计
        
        if($this->role_id==0||$this->role_id==6){
       	 	$clinicSumNum=$this->clinicModel->where(' DATE_FORMAT(add_time,"%Y")='.$year.' and type=0 and is_delete=1')->count();
        }elseif($this->role_id==5||$this->role_id==4||$this->role_id==3){
        		$clinicSumNum=$this->clinicModel->where('saleman_id ='.$sale_id.' and DATE_FORMAT(add_time,"%Y")='.$year.' and type=0 and is_delete=1')->count(); 	
        }else{
       	 	$clinicSumNum=$this->clinicModel->where('saleman_id in ('.$saleids.') and DATE_FORMAT(add_time,"%Y")='.$year.' and type=0 and is_delete=1')->count();    	
        }
        //医生数量(所有诊所)
        if($this->role_id==0||$this->role_id==6){
      	  	$doctorNum = $this->doctorModel->where(' DATE_FORMAT(date,"%Y")='.$year.' and status=0')->count();
        }else{
       		 $doctorNum = $this->doctorModel->where('clinic_id in ('.WorksummerHelper::getAllClinicss($sale_id).') and status=0  and  DATE_FORMAT(date,"%Y")='.$year)->count();
        }
        
        //拜访次数
        if($this->role_id==0||$this->role_id==6){
       	 	$salmanVisitNum = $this->salemanSignModel->where(' DATE_FORMAT(sign_date,"%Y")='.$year)->count();
        }elseif($this->role_id==5||$this->role_id==4||$this->role_id==3){
        		$salmanVisitNum = $this->salemanSignModel->where('saleman_id = '.$sale_id. ' and DATE_FORMAT(sign_date,"%Y")='.$year)->count();
        }
        else{
        	$salmanVisitNum = $this->salemanSignModel->where('saleman_id in ('.$saleids. ') and DATE_FORMAT(sign_date,"%Y")='.$year)->count();
        }
        
        //申请统计（只是下一级）
        if($this->role_id==0||$this->role_id==6){
        		$salemanApplyNum = D('saleman_apply')->where('  FROM_UNIXTIME(apply_time,"%Y")='.$year)->count();
        }elseif($this->role_id==5||$this->role_id==4||$this->role_id==3){
       	 	$salemanApplyNum = D('saleman_apply')->where('saleman_id ='.$sale_id.' and  FROM_UNIXTIME(apply_time,"%Y")='.$year)->count();
        }
        else{
       	 	$salemanApplyNum = D('saleman_apply')->where('superior_id ='.$sale_id.' and  FROM_UNIXTIME(apply_time,"%Y")='.$year)->count();
        }
        //销售数价格（针对下一级）
       // $clinicNum = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where( 'saleman_id ='.$sale_id.' and type=0 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        if($this->role_id==5||$this->role_id==4||$this->role_id==3){ //角色为业务员
	      	$clinicNum = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where( 'saleman_id ='.$sale_id.' and type=1 and status!=2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
	      	$salemanOrderNum=$clinicNum[0]['sprice']==""?0:$clinicNum[0]['sprice'];
	      }elseif($this->role_id==0||$this->role_id==6){ //角色为总部时
	      	$clinicNum = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where( '  status!=2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
	      	$salemanOrderNum=$clinicNum[0]['sprice']==""?0:$clinicNum[0]['sprice'];
	      }else{
	      	$salesNum = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where('saleman_id = '.$sale_id.' and type=1 and status!=2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
	      	$salemanOrderNum=$salesNum[0]['sprice']==""?0:$salesNum[0]['sprice'];
	      }
        //订货数量（针对下一级）
        if($this->role_id==0||$this->role_id==6){
        		$salemanPrice = $this->salemanOrderModel->field(' sum(total_price) as price')->where(' status!=2 and FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        }elseif($this->role_id==5||$this->role_id==4||$this->role_id==3){
        		$salemanPrice = $this->salemanOrderModel->field(' sum(total_price) as price')->where('saleman_id ='.$sale_id.' and type=1 and status!=2 and FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        }
        else{
        		$salemanPrice = $this->salemanOrderModel->field(' sum(total_price) as price')->where('saleman_id ='.$sale_id.' and type=1 and status!=2 and FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        }
        //退货数量
        if(!empty(WorksummerHelper::getAllChildrenId($sale_id))){
        		$quitsaleman = WorksummerHelper::getAllChildrenId($sale_id).','.$sale_id;
        }else{
        		$quitsaleman=$sale_id;
        }
         //退货金额，针对当前帐号被退货的情况
        if($this->role_id==5||$this->role_id==4||$this->role_id==3){
        		$quitePrice = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where('send_id = ( '.$sale_id.' ) and type=1 and status =2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        }elseif($this->role_id==0||$this->role_id==6){
        		$quitePrice = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where(' status =2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();	
        }else{
       	 	$quitePrice = $this->salemanOrderModel->field(' sum(total_price) as sprice')->where('send_id in ( '.$sale_id.') and type=1 and status =2 and  FROM_UNIXTIME(add_time,"%Y")='.$year)->select();
        }
        //用于下级计算
        if(empty (I('get.saleId'))){
        		$data['countycount']=$countycount==''?0:$countycount;
        		$data['citycount']=$citycount==''?0:$citycount;
        		$data['provincecount']=$provincecount==''?0:$provincecount;
        		$data['assistantcount']=$assistantcount==''?0:$assistantcount;
        		$data['saleManagercount']=$saleManagercount==''?0:$saleManagercount;
        		$data['salemanNum']=$salecount==''?0:$salecount;
        		$data['clinicNum']=$clinicSumNum==''?0:$clinicSumNum;
        		$data['clinicSumNum']=$purposeClinicNum==''?0:$purposeClinicNum;
        		$data['salmanVisitNum']=$salmanVisitNum==''?0:$salmanVisitNum;
        		$data['doctorNum']=$doctorNum==''?0:$doctorNum;
        		$data['salemanApplyNum']=$salemanApplyNum==''?0:$salemanApplyNum;
        		$data['salemanOrderNum']=$salemanOrderNum==''?0:$salemanOrderNum;
        		$data['quitePrice']= empty($quitePrice)?0:empty($quitePrice[0]['sprice'])?0:$quitePrice[0]['sprice'];
        		$data['salemanPrice']=$salemanPrice[0]['price']==''?0:$salemanPrice[0]['price'];
        		$data['role']=(int)$role==''?0:(int)$role;
        		$data['sale_id']=$this->sale_id==''?0:$this->sale_id;
        		$data['year']=$year==''?0:$year;
        		return $data;
        }else{
	        	$this->assign('countycount',$countycount==''?0:$countycount);//县总
	        	$this->assign('citycount',$citycount==''?0:$citycount);//地总
	        	$this->assign('provincecount',$provincecount==''?0:$provincecount);//省总
	        	$this->assign('assistantcount',$assistantcount==''?0:$assistantcount);//大区经理助理
	        	$this->assign('saleManagercount',$saleManagercount==''?0:$saleManagercount);//大区经理
	        	
	        	$this->assign('salemanNum',$salecount==''?0:$salecount);//业务员数量
	        	$this->assign('clinicNum',$clinicSumNum==''?0:$clinicSumNum);//诊所
	        	$this->assign('clinicSumNum',$purposeClinicNum==''?0:$purposeClinicNum);//目标诊所
	        	$this->assign('salmanVisitNum',$salmanVisitNum==''?0:$salmanVisitNum);
	        	$this->assign('salemanApplyNum',$salemanApplyNum==''?0:$salemanApplyNum);
	        	$this->assign('doctorNum',$doctorNum==''?0:$doctorNum);
	        	$this->assign('salemanOrderNum',$salemanOrderNum==''?0:$salemanOrderNum);
	        	$this->assign('quitePrice',empty($quitePrice)?0:empty($quitePrice[0]['sprice'])?0:$quitePrice[0]['sprice']);
	        	$this->assign('salemanPrice',$salemanPrice[0]['price']==''?0:$salemanPrice[0]['price']);
	        	$this->assign('role',(int)$role);
	        	
	        	$this->assign('sale_id',$this->sale_id);
	        	$this->assign('role',$this->role_id);
	        	$this->assign('year',$year);
	        	//动态生成年份
	        	for($i=1;$i<=4;$i++){
	        		$str= '-'.($i-1).' year';
	        		$yearArray[(intval($i)-1)] = date("Y", strtotime($str));
	        	}
	        	$this->assign('years',$yearArray);
	        	if($this->role_id==5){//业务员界面
	        		$this->display('salemanindex');
	        	}else{//其他角色界面
	        		$this->display();
	        	}
	        
        }
        
    }

    /*
     *
     * 业务员列表
     *
     * */
    public function salelist()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
     	$sales = WorksummerHelper::getAllChildrenId($sale_id);
        $saleids = $sales.",".$sale_id;
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        if($role==0||$role==6){
        		$countys = $this->salemanModel->where(' role_id=5 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
        }else{
        		$countys = $this->salemanModel->where('id in ('.$saleids.') and (role_id=5 ) and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
        }
        foreach ($countys as $k=>$v){		
        		$countys[$k]['stastics']=$this->index($v['id'],$year);
        		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
        }
        $this->assign('salelists',$countys);
        $this->assign('count',empty($countys)?0:count($countys));
        $this->assign('year',$year);
        $this->assign('sale_id',think_ucenter_encrypt($sale_id));
        $this->display();
    }

    /*
     *
     * 地总中县总列表
     *
     * */
    public function countrylist()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
        $sales = WorksummerHelper::getAllChildrenId($sale_id);
        $saleids = $sales.",".$sale_id;
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        if($role==0||$role==6){
        		$countys = $this->salemanModel->where(' role_id=4 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总    
        }else{
       	 	$countys = $this->salemanModel->where('id in ('.$saleids.') and role_id=4 and status=0 and FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总   
        }
         foreach ($countys as $k=>$v){
        		$countys[$k]['stastics']=$this->index($v['id'],$year);
        		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
        }
        $this->assign('countrylists',$countys);
        $this->assign('count',empty($countys)?0:count($countys));
        $this->assign('year',$year);
        $this->assign('sale_id',think_ucenter_encrypt($sale_id));
        $countys[$k]['id'] = think_ucenter_encrypt($v['id']);
        $this->display();
    }

    /*
     *
     * 省总中的地总列表
     *
     * */
    public function arealist()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
       	$sales = WorksummerHelper::getAllChildrenId($sale_id);
        $saleids = $sales.",".$sale_id;
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        if($role==0||$role==6){
        		$countys = $this->salemanModel->where(' role_id=3 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
        }else{
       	 	$countys = $this->salemanModel->where('id in ('.$saleids.') and role_id=3 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总	
        }
        foreach ($countys as $k=>$v){
        		$countys[$k]['stastics']=$this->index($v['id'],$year);
        		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
        }
        
        $this->assign('countrylists',$countys);
        $this->assign('count',empty($countys)?0:count($countys));
        $this->assign('year',$year);
        $this->assign('sale_id',think_ucenter_encrypt($sale_id));
        $this->display();
    }
    
    /*
     *
     * 大区经理中的地总列表
     *
     * */
    public function provincelist()
    {
    	$sale_id = I('get.sale_id');
    	$year = I('get.year');
    	$sales = WorksummerHelper::getAllChildrenId($sale_id);
    	$role = $this->salemanModel->getRoleBySaleId($sale_id);
    	$saleids = $sales.",".$sale_id;
    	if($role==0||$role==6){
    		$countys = $this->salemanModel->where('role_id=2 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
    		
    	}else{
    		$countys = $this->salemanModel->where('id in ('.$saleids.') and role_id=2 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
    	}
    foreach ($countys as $k=>$v){
    		$countys[$k]['stastics']=$this->index($v['id'],$year);
    		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
    	}
    
    	$this->assign('countrylists',$countys);
    	$this->assign('count',empty($countys)?0:count($countys));
    	$this->assign('year',$year);
    	$this->assign('sale_id',think_ucenter_encrypt($sale_id));
    	$this->display();
    }
    /*
     *
     * 大区经理中的助理
     *
     * */
    public function salemanagerassistantlist()
    {
    	$sale_id = I('get.sale_id');
    	$year = I('get.year');
    	$sales = WorksummerHelper::getAllChildrenId($sale_id);
    	$role = $this->salemanModel->getRoleBySaleId($sale_id);
    	$saleids = $sales.",".$sale_id;
    	if($role==0||$role==6){
    		$countys = $this->salemanModel->where('role_id=11 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
    		
    	}else{
    		$countys = $this->salemanModel->where('id in ('.$saleids.') and role_id=11 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
    	}
    	foreach ($countys as $k=>$v){
    		$countys[$k]['stastics']=$this->index($v['id'],$year);
    		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
    	}
    	
    	$this->assign('countrylists',$countys);
    	$this->assign('count',empty($countys)?0:count($countys));
    	$this->assign('year',$year);
    	$this->assign('sale_id',think_ucenter_encrypt($sale_id));
    //	$this->display('salemanagerlist');
    	$this->display('salemanagerassistantlist');
    }
    /*
     *
     * 大区经理列表
     *
     * */
    public function salemanagerlist()
    {
    	$sale_id = I('get.sale_id');
    	$year = I('get.year');
    	$sales = WorksummerHelper::getAllChildrenId($sale_id);
    	$saleids = $sales.",".$sale_id;
    	$countys = $this->salemanModel->where(' role_id=1 and  FROM_UNIXTIME(add_time,"%Y")='.$year.' and status=0')->select();//获取所有县总
    	foreach ($countys as $k=>$v){
    		$countys[$k]['stastics']=$this->index($v['id'],$year);
    		$countys[$k]['id'] = think_ucenter_encrypt($v['id']);
    	}
    	$this->assign('countrylists',$countys);
    	$this->assign('count',empty($countys)?0:count($countys));
    	$this->assign('year',$year);
    	$this->assign('sale_id',think_ucenter_encrypt($sale_id));
    	$this->display();
    }

    /*
     * 
     * 目标诊所
     * 
     * */
    public function clinicsum()
    {
        $year = I('get.year');
        $sale_id = I('get.sale_id');
        $this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
        if($this->role_id==5||$this->role_id==4||$this->role_id==3){
       	 	$this->clinicMod = new \Home\Model\SaleManager\Clinic\ClinicModel();
	        	$lists = $this->clinicMod->findInfobyCondition(array('saleman_id'=>$sale_id,'type'=>C('SALEMANAGER_CLINIC_TYPE.AIM'),'is_delete'=>C('SALEMANAGER_CLINIC_IDDELETE.NORMAL')),C('SALEMANAGER_PAGE_NUM'),0,'add_time desc');  
	        	foreach ($lists as $k=>$v){
	        		$map = D('doctor')->where('clinic_id='.$v['id'])->field('name')->find();
	        		$lists[$k]['id']=think_ucenter_encrypt($v['id']);
	        		$lists[$k]['manager_name']=$map['name'];
	        	}
        		$this->assign('clinics',$lists);
        		$this->display('allclinic');
        		exit;
        }
        $clinic_str = WorksummerHelper::getCliincByMonth($sale_id, $year,1,$this->role_id);//目标诊所
        $this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
        $this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
        $this->display();
    }
    
    
    /*
     *
     * 诊所数量
     *
     * */
    public function trueclinicsum()
    {
    	$year = I('get.year');
    	$sale_id = I('get.sale_id');
    	$this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
    	if($this->role_id==5||$this->role_id==4||$this->role_id==3){
    		$this->clinicMod = new \Home\Model\SaleManager\Clinic\ClinicModel();
    		$lists = $this->clinicMod->findInfobyCondition(array('saleman_id'=>$sale_id,'type'=>C('SALEMANAGER_CLINIC_TYPE.NORMAL'),'is_delete'=>C('SALEMANAGER_CLINIC_IDDELETE.NORMAL')),C('SALEMANAGER_PAGE_NUM'),0,'add_time desc');
    		foreach ($lists as $k=>$v){
    			if(intval($year)!=intval(date('Y',strtotime($v['add_time'])))){
    				unset($lists[$k]);
    				continue; 
    			} 
    			$map = D('doctor')->where('clinic_id='.$v['id'])->field('name')->find();
    			$lists[$k]['id']=think_ucenter_encrypt($v['id']);
    			$lists[$k]['manager_name']=$map['name'];
    		}
    		$this->assign('clinics',$lists);
    		$this->display('trueclinic');
    		exit;
    	}else{
    		$clinic_str = WorksummerHelper::getCliincByMonth($sale_id, $year,0,$this->role_id);//诊所
    		$this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
    		$this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
    		$this->display();
    	}
    
    }
    
    /**
     * 医生数量
     */
    public function doctornum(){
	    	$year = I('get.year');
	    	$sale_id = I('get.sale_id');
	    	$this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
	    	if($this->role_id==5){
	    		//$clinics = WorksummerHelper::getAllClinicss($sale_id, $year);
	    		$doctors = $this->doctorModel->where('clinic_id in ('.WorksummerHelper::getAllClinicss($sale_id).') and status=0  and  DATE_FORMAT(date,"%Y")='.$year)->select();
	    		foreach ($doctors as $k=>$v){
	    			$doctors[$k]['clinic_name']=D('clinic')->field('clinic_name')->where('id='.$v['clinic_id'])->find()['clinic_name'];
	    		}
	    		$this->assign('doctors',$doctors);
	    		$this->display('alldoctor');
	    		exit;
	    	}else{
		    	$clinic_str = WorksummerHelper::getAllDoctor($sale_id, $year,$this->role_id);//医生数量
		    	$this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
		    	$this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
		    	$this->display();
	    	}
    }
    
    /*
     *
     * 业务员下诊所详情
     *
     * */
    public function allclinic()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');

        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);

        if( $role == 5 )//业务员
        {
            $clinics = $this->clinicModel->getClinicsBySalemanID($sale_id,$year);
        }

        if( $role == 4 )//县总下的诊所
        {
            $clinics = $this->clinicModel->getClinicsByXianZong($sale_id,$year);
        }

        if( $role == 3) //地总下的诊所
        {
            $clinics = $this->clinicModel->getClinicsByDiZong($sale_id,$year);

        }
        $this->assign('clinics',$clinics);
        $this->display();
    }

    /*
     *
     * 业务员下的诊所中医生
     *
     * */
    public function alldoctor()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');

        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);

        if( $role == 5 )//业务员下的医生
        {
            $doctors = $this->getAllDoctorsBySalemanID($sale_id,$year);
        }

        if( $role == 4 )//县总下的医生
        {
            $doctors = $this->doctorModel->getAllDoctorsByXianZong($sale_id,$year);
        }

        if( $role == 3 )//地总下的医生
        {
            $doctors = $this->doctorModel->getAllDoctorsByDiZong($sale_id,$year);
        }
        $this->assign('doctors',$doctors);
        $this->display();
    }
    
    
    /*
     * 
     * 拜访次数
     * 
     * */
    public function visitalltimes()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');

        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        if($role==5||$role==4||$role==3){
//         	if(empty(WorksummerHelper::getAllChildrenId($sale_id))){
//         		$condition=$sale_id;
//         	}else{
//         		$condition=WorksummerHelper::getAllChildrenId($sale_id).",".$sale_id;
//         	}
        	$sales =$this->salemanSignModel->field('count(id) as ids,DATE_FORMAT(sign_date,"%m") as m ')->where('saleman_id = ('.$sale_id.') and  DATE_FORMAT(sign_date,"%Y")='.$year)->group('m')->select();
			$total=0;
			for($i=1;$i<=12;$i++){
				foreach ($sales as $k=>$v){
					if(intval($v['m'])==$i){
						$total+=$v['ids'];
						$salmanVisitMonth[$i]=$v['ids'];
					}
				}
				if(empty($salmanVisitMonth[$i])){
					$salmanVisitMonth[$i]=0;
				}
			}
			$this->assign('salmanVisitNum',$total);
	        $this->assign('salmanVisitMonth',$salmanVisitMonth);
	        $this->assign('year',$year);
	        $this->assign('sale_id',$sale_id);
	        $this->display();
	    	}else{
	       	$clinic_str = WorksummerHelper::getVisitsCount($sale_id, $year,$role);
	    		$this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
	    		$this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
	    		$this->display('visitsum');
	    	}
    }

    /*
     *
     * 拜访详情
     *
     * */
    public function visitdetail()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
        $month = I('get.month');
        $data = $this->salemanSignModel->getMonthVisitDetailBySaleId($sale_id,$year,$month);
        $this->assign('year',$year);
        $this->assign('month',$month);
        $this->assign('data',$data);
        $this->assign('sale_id',$sale_id);
        $this->display();
    }
    

    /*
     * 申请统计
     * */
    public function applicatemeet()
    {
        $sale_id = I('get.sale_id');
        $type = I('get.type');
        $year = I('get.year');
        if(!empty($type)){
        		$types="  and type=".$type;
        }
        $this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
        if($this->role_id==5||$this->role_id==4||$this->role_id==3){
	        $lists=D('saleman_apply')->field('apply_time,type,reply_status')->where('saleman_id='.$sale_id." ".$types.'and  FROM_UNIXTIME(apply_time,"%Y")='.$year)->select();
	        	   	foreach ($lists as $k=>$v){
	        	   		$lists[$k]['apply_time'] = date ( 'Y-m-d', $v['apply_time'] );
	        	   		$lists[$k]['type']=SalemanagerHelper::traMetting($v['type']);
	        	   		$lists[$k]['reply_status']=SalemanagerHelper::convertApplyStatus($v['reply_status']);
	        		}
	        	$types = C('SALEMANAGER_APPLY_NAME');
	        	$this->assign('types',$types);
	        	$this->assign('type',$type);
	        	$this->assign('lists',$lists);
	        	$this->display();
	        	exit;
        }else{
	        $clinic_str = WorksummerHelper::getApplyNum($sale_id,$year,$this->role_id);
	        $this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
		    	$this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
	        $this->display('applicatesum');
        }
    }

    /*
     *
     * 销售统计
     *
     * */
    public function salesumary()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
   
        $this->role_id = $this->salemanModel->field('role_id')->where('id='.$sale_id)->find()['role_id'];
        if($this->role_id==5||$this->role_id==4||$this->role_id==3){//业务员显示
       	 	$lists=D('saleman_order')->field('sum(total_price) as total_price,count(id) as num,send_id')->where('saleman_id='.$sale_id.' and  FROM_UNIXTIME(add_time,"%Y")='.$year." and type=0  and status!=2")->group('send_id')->select();
	        	foreach ($lists as $k=>$v){
	        		$clinics=D('clinic')->field('clinic_name,id')->where('id='.$v['send_id'])->find();
	        		$lists[$k]['name']=$clinics['clinic_name'];
	        		$lists[$k]['clinic_id']=$clinics['id'];
	        	}
	        	$this->assign('sale_id',$sale_id);
	        	$this->assign('orders',$lists);
	        	$this->assign('year',$year);
	        	$this->display();
	        	exit;
        }else{
        		//其他角色
	        $clinic_str = WorksummerHelper::getSaleNum($sale_id,$year,$this->role_id);
	        $this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
	        $this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
	        $this->display('salesumarysum');
        }
    }

    /*
     *
     * 根据诊所id来确定每单的详情
     *
     * */
    public function orderdetail()
    {
        $year = I('get.year');
        $sale_id = I('get.sale_id');
        $clinic_id = I('get.clinic_id');
        $order_id = I('get.order_id');
        $orders = D('saleman_order')->where('saleman_id='.$sale_id.'  and send_id='.$clinic_id.' and  FROM_UNIXTIME(add_time,"%Y")='.$year." and type=0  and status!=2")->select();
        foreach ($orders as $k=>$v){
        		$orders[$k]['status'] = $orders[$k]['status']==0?'待处理':'已完成';
        }
        $this->assign('orders',$orders);
        $this->display();
    }

    /*
     *
     * 订货统计
     *
     * */
    public function orderlist()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);

        if( $role == 5||$role == 4||$role ==3)
        {	$price=0;
        		$orders=D('saleman_order')->where('saleman_id='.$sale_id.' and  FROM_UNIXTIME(add_time,"%Y")='.$year." and type=1  and status!=2")->select();
        		foreach ($orders as $k=>$v){
        			$orders[$k]['real_name'] = D('saleman')->field('real_name')->where('id='.$v['send_id'])->find()['real_name'];
        			$orders[$k]['status'] = $orders[$k]['status']==0?'待处理':'已完成';
        			$price+=$orders[$k]['total_price'];
        			$orders[$k]['oid']=think_ucenter_encrypt($orders[$k]['id']);
        		}
        		$num = empty($orders) ?0 :  count($orders);
            $price = $price ? $price : 0;
            unset($orders['num']);
            unset($orders['price']);
            $this->assign('orders',$orders);
            $this->assign('num',$num);
            $this->assign('price',$price);
            $this->assign('year',$year);
            $this->assign('sale_id',$sale_id);
            $this->display();
            exit;
        }else{
	        	$clinic_str = WorksummerHelper::getOrderNum($sale_id,$year,$role);
	        	$this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
	        	$this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
	        	$this->display('ordersum');
        }
       
    }

    /*
     *
     * 退货统计
     *
     * */
    public function quitsale()
    {
        $sale_id = I('get.sale_id');
        $year = I('get.year');
        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        if( $role == 5||$role == 4||$role == 3)
        {
            //$orders = $this->salemanOrderModel->getQuiteSaleClinicListBySaleId($sale_id,$year);
        		$orders = $this->salemanOrderModel->where(array('send_id'=>$sale_id,'status'=>2))->select();
            $this->assign('orders',$orders);
            $this->assign('year',$year);
            $this->assign('sale_id',$sale_id);
            $this->display();
            exit;
        }
        $clinic_str = WorksummerHelper::getReturnOrderNum($sale_id,$year,$role);
        $this->assign('clinicSumNum',empty($clinic_str['total'])?0:$clinic_str['total']);
        $this->assign('clinic_str','['.implode(',',$clinic_str["res"]).']');
        $this->display('quitsum');
       
    }

    /*
     * 
     * 退货统计详情
     * 
     * */
    public function quitsaledetail()
    {
        $year = I('get.year');
        $sale_id = I('get.sale_id');
        $clinic_id = I('get.clinic_id');
        $orders = $this->salemanOrderModel->getQuiteClinicListsByClinicId($clinic_id,$year);
        $this->assign('orders',$orders);
        $this->display();
    }
    /*
     *
     * 业务员下诊所地区显示
     *
     * */
    public function salemap()
    {
        $year = I('get.year');
        $sale_id = I('get.sale_id');
        //获取角色  角色,0：总部，1：大区经理；2：省总；3：地总；4：县总；5：业务员
        $role = $this->salemanModel->getRoleBySaleId($sale_id);
        $positions = $this->clinicModel->getPositionBySalemanID($sale_id,$year,$role);
        $positions = json_encode($positions);
        $this->assign('positions',$positions);
        $this->display();
    }

    /*
     *
     * 获取所有医生
     *
     * */
    public function getAllDoctorsBySalemanID($sale_id,$year)
    {
        $clinic_ids = $this->clinicModel->getClinicidsBySalemanID($sale_id,$year);
        if( !$clinic_ids )
        {
            return false;
        }
        $doctors = $this->doctorModel->getAllDoctorsByIds($clinic_ids);
        foreach( $doctors as &$doctor )
        {
            $doctor['clinic_name'] = $this->clinicModel->getClinicNameById($doctor['clinic_id']);
        }
        return $doctors;
    }


    /*
     *
     *
     * 删除医生
     *
     * */
    public function removedDoctors()
    {
        $doctors = $this->doctorModel->getRemovedDoctors();
        var_dump($doctors);
    }
    
    
    public function orderInfo(){
    	$REQUEST = ( array ) I ( 'request.' );
    	if(IS_POST){
    			
    	}else{
    		if(!empty($REQUEST['oid'])){
    			$info = M('saleman_order_detail')->where(array('order_id'=>think_ucenter_decrypt($REQUEST['oid'])))->select();
    			foreach ($info as $k=>$v){
    				$total+=$v['total_price'];
    			}
    		}
    		$this->assign('total',$total);
    		$this->assign('oid',$REQUEST['oid']);
    		$this->assign('status',$REQUEST['status']);
    		$this->assign('info',$info);
    		$this->display();
    	}
    }
}